package com.atguigu.bigdata.edu.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.bigdata.edu.realtime.app.BaseAppV1;
import com.atguigu.bigdata.edu.realtime.bean.ExamPaperBean;
import com.atguigu.bigdata.edu.realtime.common.Constant;
import com.atguigu.bigdata.edu.realtime.util.AtguiguUtil;
import com.atguigu.bigdata.edu.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.math.BigDecimal;
import java.time.Duration;

public class Dws_13_DwsExamPaperCountWindow extends BaseAppV1 {
    public static void main(String[] args) {
        new Dws_13_DwsExamPaperCountWindow().init(
                4013,
                2,
                "Dws_13_DwsExamPaperCountWindow",
                Constant.DWD_EXAM_PAPER_DETAIL
        );
    }

    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {

        // 1.将数据转换成POJO
        SingleOutputStreamOperator<ExamPaperBean> beanStream = parseToObj(stream);
        // 2.对数据分组 开窗 聚合
        SingleOutputStreamOperator<ExamPaperBean> windowAndAggedStream = windowAndAgg(beanStream);
        // 3.将数据写入ClickHouse
        writeToClickhouse(windowAndAggedStream);
    }

    private void writeToClickhouse(SingleOutputStreamOperator<ExamPaperBean> windowAndAggedStream) {
        windowAndAggedStream.addSink(FlinkSinkUtil.getClickHouseSink("dws_exam_paper_score_window", ExamPaperBean.class));
    }

    private SingleOutputStreamOperator<ExamPaperBean> windowAndAgg(SingleOutputStreamOperator<ExamPaperBean> beanStream) {
        return beanStream
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<ExamPaperBean>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                                .withTimestampAssigner( (examPaperBean, ts) -> examPaperBean.getTs()))
                .keyBy(examPaperBean -> examPaperBean.getPaperId())
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(
                        new ReduceFunction<ExamPaperBean>() {
                            @Override
                            public ExamPaperBean reduce(ExamPaperBean b1, ExamPaperBean b2) throws Exception {
                                b1.setUserCount(b1.getUserCount() + b2.getUserCount());
                                b1.setScoreCount(b1.getScoreCount().add(b2.getScoreCount()));
                                b1.setDurCount(b1.getDurCount() + b2.getDurCount());
                                return b1;
                            }
                        },
                        new ProcessWindowFunction<ExamPaperBean, ExamPaperBean, Long, TimeWindow>() {
                            @Override
                            public void process(Long aLong, Context context, Iterable<ExamPaperBean> iterable, Collector<ExamPaperBean> collector) throws Exception {
                                ExamPaperBean bean = iterable.iterator().next();
                                bean.setStt(AtguiguUtil.toDateTime(context.window().getStart()));
                                bean.setEdt(AtguiguUtil.toDateTime(context.window().getEnd()));
                                bean.setTs(System.currentTimeMillis());
                                collector.collect(bean);
                            }
                        });
    }

    private SingleOutputStreamOperator<ExamPaperBean> parseToObj(DataStreamSource<String> stream) {
        return stream.map(new RichMapFunction<String, ExamPaperBean>() {
            @Override
            public ExamPaperBean map(String s) throws Exception {
                JSONObject obj = JSON.parseObject(s);
                Long paperId = obj.getLong("paper_id");
                String courserId = obj.getString("course_id");
                String paperTitle = obj.getString("paper_title");
                Long userCount = 1L;
                BigDecimal scoreCount = obj.getBigDecimal("score");
                Long durCount = obj.getLong("duration_sec");
                return new ExamPaperBean("","",paperId,courserId,paperTitle,userCount,scoreCount,durCount, AtguiguUtil.dateToTs(obj.getString("submit_time")));
            }
        });
    }

}
